package aerouacmsla.interfazDeUsuario;

import aerouacmsla.procedimientos.BDConexion;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author Marco
 */
public class MonitorDeConexion extends javax.swing.JDialog {

    private BDConexion miConexion;

    public MonitorDeConexion() {
        initComponents();
    }

    public MonitorDeConexion(BDConexion miConexion) {
        initComponents();
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(d.width - getSize().width, d.height- getSize().height);
        setMiConexion(miConexion);
        bReconectar.setVisible(false);
        Timer timer = new Timer (3000, new ActionListener () {
            public void actionPerformed(ActionEvent e) {
                if (!getMiConexion().estadoDeLaConexion()){
                    //Desconectado
                    conexionLabel.setBusy(false);
                    informacion.setText("Sin Respuesta");
                    informacion.setForeground(new Color(204 , 0, 0));
                    bReconectar.setVisible(true);
                }
                else {
                    //Conectado sin problemas
                    if (!conexionLabel.isBusy()) {
                        conexionLabel.setBusy(true);
                        informacion.setText("Conectado Al Servidor");
                        informacion.setForeground(new Color(0 , 102, 0));
                        bReconectar.setVisible(false);
                    }
                }
            }
         });
         timer.start();
    }

    public BDConexion getMiConexion() {
        return miConexion;
    }

    public void setMiConexion(BDConexion miConexion) {
        this.miConexion = miConexion;
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        backgroundJPanel1 = new org.neocs.beans.panel.BackgroundJPanel();
        informacion = new org.jdesktop.swingx.JXLabel();
        conexionLabel = new org.jdesktop.swingx.JXBusyLabel();
        bReconectar = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Monitor");
        setAlwaysOnTop(true);
        setUndecorated(true);

        backgroundJPanel1.setFondoImagenIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/FondoMonitor.png"))); // NOI18N
        backgroundJPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                backgroundJPanel1MouseDragged(evt);
            }
        });

        informacion.setForeground(new java.awt.Color(0, 102, 0));
        informacion.setText("Conectado Al Servidor");

        conexionLabel.setForeground(new java.awt.Color(255, 255, 255));
        conexionLabel.setText("Estado:");
        conexionLabel.setBusy(true);

        bReconectar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/reconnect.png"))); // NOI18N
        bReconectar.setToolTipText("Reconectar");
        bReconectar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        bReconectar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bReconectarMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout backgroundJPanel1Layout = new javax.swing.GroupLayout(backgroundJPanel1);
        backgroundJPanel1.setLayout(backgroundJPanel1Layout);
        backgroundJPanel1Layout.setHorizontalGroup(
            backgroundJPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, backgroundJPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(conexionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(informacion, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bReconectar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        backgroundJPanel1Layout.setVerticalGroup(
            backgroundJPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(backgroundJPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(backgroundJPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(backgroundJPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(backgroundJPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(conexionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(informacion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(bReconectar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(12, 12, 12))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(backgroundJPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(backgroundJPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backgroundJPanel1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_backgroundJPanel1MouseDragged
        int x = getLocation().x;
        int y = getLocation().y;
        int w = evt.getX();
        int h = evt.getY();
        Point nuevo = new Point(x+w, y+h);
        this.setLocation(nuevo);
    }//GEN-LAST:event_backgroundJPanel1MouseDragged

    private void bReconectarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bReconectarMouseClicked
        getMiConexion().conectarBD();
    }//GEN-LAST:event_bReconectarMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MonitorDeConexion().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel bReconectar;
    private org.neocs.beans.panel.BackgroundJPanel backgroundJPanel1;
    private org.jdesktop.swingx.JXBusyLabel conexionLabel;
    private org.jdesktop.swingx.JXLabel informacion;
    // End of variables declaration//GEN-END:variables

}
